<!--
 @description 首页
 @fileName index.vue
 @author zengqiongying
 @created 2023/04/13 13:16:46
-->
<template>
  <div>
    <div class="banner">
      <img src="@/assets/images/home.png" />
    </div>
    <div class="home-nav-container">
      <div v-for="(item, i) in list" :key="i" class="nav-group">
        <div class="g-title">{{ item.name }}</div>
        <div class="g-list">
          <div v-for="(l, k) in item.list" :key="k" class="item" @click="goToPage(l.routeName)">
            <img class="icon" :src="require(`@/assets/images/home/${l.routeName}.png`)" />
            <div>{{ l.name }}</div>
          </div>
        </div>
      </div>
    </div>
    <div class="search-container">
      <div class="search-input" @click="toSearch">搜索知识库文件</div>
      <FileList is-home />
      <div class="more" @click="toSearch">查看更多</div>
    </div>
  </div>
</template>
<script lang="ts">
  import { useRouter } from 'vue-router'
  import FileList from './components/FileList.vue'
  import { ref, reactive, toRefs } from 'vue'
  import { queryBtnList } from '@/apis/dataDict'

  interface type {
    list: any
  }
  export default {
    name: 'Index',
    components: { FileList },
    setup() {
      const $router = useRouter()
      const datas = reactive<type>({
        list: []
      })

      const moduleData = [
        {
          name: '市场开发及整改',
          list: [{ buttonCode: 'weektask', name: '周任务跟进', routeName: 'StoreUpgradeTask' }]
        },
        {
          name: '招商管理',
          list: [
            { buttonCode: 'potentialcustomer', name: '意向客户', routeName: 'PotentialCustomer' },
            { buttonCode: 'blankmarket', name: '空白市场', routeName: 'BlankMarket' },
            { buttonCode: 'leasingmanager', name: '经理管理', routeName: 'LeasingManager' },
            { buttonCode: 'custfollowup', name: '意向跟进', routeName: 'PotentialCustomerFollowUp' },
            { buttonCode: 'marketchange', name: '市场变更', routeName: 'PotentialCustomerMarketChange' }
          ]
        },
        {
          name: '经销商档案管理',
          list: [
            { buttonCode: 'view', name: '档案查询', routeName: 'DealerInfoCustomer' },
            { buttonCode: 'newcustomer', name: '新建档案', routeName: 'DealerInfoNewArchive' },
            { buttonCode: 'newshop', name: '新增门店', routeName: 'DealerInfoNewShop' },
            { buttonCode: 'businesschange', name: '业务类型变更', routeName: 'DealerInfoShopChange' },
            { buttonCode: 'shopclose', name: '分店关店', routeName: 'DealerInfoShutUpShop' },
            { buttonCode: 'termination', name: '合作终止', routeName: 'DealerInfoTermination' },
            { buttonCode: 'archivechange', name: '档案变更', routeName: 'DealerInfoArchiveChange' },
            { buttonCode: 'freezeandthaw', name: '冻结/解冻', routeName: 'DealerInfoFreezeAndthaw' }
          ]
        }
      ]
      const goToPage = (name: string) => {
        if (name) {
          $router.push({ name })
        }
      }
      const toSearch = () => {
        $router.push({
          path: '/fileSearch'
        })
      }

      const getBtnList = async (code: string) => {
        const res: any = await queryBtnList(code)
        const { success, data = [] } = res
        if (success === 1) {
          let newArr = data.map((i: any) => i.buttonCode)
          const list: any = moduleData
            .map((v) => {
              const filterList = v.list.filter((vv: any) => newArr.includes(vv.buttonCode))
              if (filterList.length) {
                return {
                  name: v.name,
                  list: filterList
                }
              }
            })
            .filter((v) => v)

          datas.list = list
        }
      }
      getBtnList('home')

      return {
        ...toRefs(datas),
        goToPage,
        toSearch
      }
    }
  }
</script>
<style lang="less" scoped>
  .banner {
    max-height: 254px;
    overflow: hidden;
    img {
      width: 100%;
      height: auto;
    }
  }
  .home-nav-container {
    position: relative;
    z-index: 1;
    margin-top: -50px;
    padding: 8px 0 14px;
    background: #fff;
    min-height: 60px;
    border-radius: 16px 16px 0px 0px;
    .nav-group {
      padding: 12px 0 14px;
      .g-title {
        padding-left: 14px;
        height: 22px;
        font: var(--font-card-title);
      }
      .g-list {
        display: flex;
        flex-wrap: wrap;
        .item {
          margin-top: 18px;
          width: 20%;
          height: 66px;
          text-align: center;
          .icon {
            width: 32px;
            height: 32px;
          }
          .name {
            margin-top: 8px;
            font: var(--font-point);
          }
        }
      }
    }
  }
  .search-container {
    position: relative;
    background: #fff;
    .search-input {
      margin: 0 14px;
      padding: 8px 16px;
      height: 36px;
      box-sizing: border-box;
      border-radius: 43px;
      background: rgba(242, 244, 243, 0.9);
      border: 1px solid var(--divisionline-normal);
      font: var(--font-main-body);
      color: var(--texticon-default-normal);
    }
    .more {
      position: absolute;
      bottom: 20px;
      left: 0;
      z-index: 1;
      width: 100%;
      background: #fff;
      font: var(--font-main-body);
      color: var(--texticon-default-normal);
      text-align: center;
      line-height: 47px;
    }
  }
</style>
